#define _CRT_SECURE_NO_WARNINGS
#include"Date.h"

int Date::Getmonth_day()
{
	static int monthday[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	if (_month == 2 && ((_year % 4 == 0 && _year % 100 != 0) || (_year % 400 == 0)))
		return 29;
	return monthday[_month];
}
Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
}
Date::Date(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}
Date& Date::operator=(const Date& d)
{
	if (this != &d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}

	return *this;
}
Date::~Date()
{
	;
}
Date Date::operator+=(int day)
{
	if (day < 0)
	{
		return *this -= -day;
	}
	_day += day;
	while (_day > Getmonth_day())
	{
		_day -= Getmonth_day();
		_month++;
		if (_month == 13)
		{
			_year++;
			_month = 1;
		}
	}
	return *this;
}
Date Date::operator+(int day)
{
	Date tmp = *this;
	tmp += day;
	return tmp;
}

Date Date::operator-=(int day)
{
	if (day < 0)
	{
		return *this += -day;
	}
	_day -= day;
	while (_day <= 0)
	{
		_month--;
		if (_month == 0)
		{
			_year--;
			_month = 12;
		}
		_day += Getmonth_day();
	}
	return *this;
}
Date Date::operator-(int day)
{
	Date tmp = *this;
	tmp -= day;
	return tmp;
}
Date Date::operator++()
{
	*this += 1;
	return *this;

}
Date Date::operator++(int)
{
	Date tmp = *this;
	*this += 1;
	return tmp;
}
Date Date::operator--()
{
	*this -= 1;
	return *this;
}
Date Date::operator--(int)
{
	Date tmp = *this;
	*this -= 1;
	return tmp;
}
bool Date::operator>=(const Date& d)
{
	return (_year > d._year ||
		_year == d._year && _month > d._month ||
		_year == d._year && _month == d._month && _day >= d._day);

}
bool Date::operator<(const Date& d)
{
	return !(*this >= d);
}
bool Date::operator==(const Date& d)
{
	return _year == d._year && _month == d._month && _day == d._day;
}
bool Date::operator<=(const Date& d)
{
	return (*this < d) || (*this == d);
}
bool Date::operator!= (const Date& d)
{
	return !(*this == d);
}
int Date::operator-(const Date& d)
{
	int day = 0;
	int flag = 1;
	Date max = *this;
	Date min = d;
	if (max < min)
	{
		max = d;
		min = *this;
	}
	while (min != max)
	{
		min++;
		day++;
	}
	return day * flag;

}

void Date::Print() const
{
	cout << _year << "/" << _month << "/" << _day << endl;
}

ostream& operator<<(ostream& cout, const Date& d)
{
	cout << d._year << "/" << d._month << "/" << d._day << endl;

	return cout;
}
